package io.github.novaframe.web;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Melvin
 * @since 2017-12-11 16:21
 */
@ControllerAdvice
public class ExceptionViewTranslator {

    public static final String DEFAULT_ERROR_VIEW = "error";

    @ExceptionHandler(value = Exception.class)
    public ModelAndView defaultErrorHandler(Exception ex, HttpServletRequest req) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("exception", ex);
        mv.addObject("url", req.getRequestURL());
        mv.setViewName(DEFAULT_ERROR_VIEW);

        return mv;
    }

}
